package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.dao.ParamDao;
import com.gxa.dao.impl.ParamDaoImpl;
import com.gxa.pojo.Basicinfo;
import com.gxa.pojo.Department;
import com.gxa.pojo.Role;
import com.gxa.service.ParamService;

import javax.servlet.annotation.WebServlet;
import java.sql.SQLException;
import java.util.List;

public class ParamServiceImpl implements ParamService {
    private ParamDao paramDao = new ParamDaoImpl();
    @Override
    public JsonResult queryparamlist(Integer currentpage, Integer pagesize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer p1 = (currentpage-1)*pagesize;
            Integer p2 = pagesize;
            Integer totalsize = paramDao.countParam();
            List<Basicinfo> basicinfos = paramDao.queryparamlist(p1, p2);
            if (basicinfos!=null){
                jsonResult.setCode(0);
                jsonResult.setCount(totalsize);
                jsonResult.setMsg("查询成功！");
                jsonResult.setData(basicinfos);
            }
        }catch (SQLException e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public JsonResult queryparamlist(Integer currentpage, Integer pagesize, String describes) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer p1 = (currentpage-1)*pagesize;
            Integer p2 = pagesize;
            Integer totalsize = paramDao.countParam(describes);
            List<Basicinfo> basicinfos = paramDao.queryparamlist(p1, p2,describes);
            if (basicinfos!=null){
                jsonResult.setCode(0);
                jsonResult.setCount(totalsize);
                jsonResult.setMsg("查询成功！");
                jsonResult.setData(basicinfos);
            }
        }catch (SQLException e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public R delBasicById(String[] ids) {
        R r = new R();
        try {
            if(ids.length>0) {
                for (String id : ids) {
                    paramDao.delInfo(id);
                }
            }
            r.setCode(200);
            r.setMsg("删除成功!");
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R addEmp(Basicinfo basicinfo) {
        R r = new R();
        try {
            Integer integer = paramDao.addInfo(basicinfo);
            if(integer>0){
                r.setCode(200);
                r.setMsg("添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R updateUser(Basicinfo basicinfo) {
        R r = new R();
        try {
            Integer result = paramDao.updateInfo(basicinfo);
            if(result>0){
                r.setCode(200);
                r.setMsg("修改成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }
}
